/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package util;

import java.io.IOException;
import javax.faces.application.FacesMessage;
import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 *
 * @author ronaldomartins
 */
public class UtilWeb {

    public static void enviarMessagemErro(String id,String menssagem){
        FacesContext.getCurrentInstance().addMessage(id, new FacesMessage(FacesMessage.SEVERITY_ERROR,menssagem,""));        
    }    

    public static void enviarMessagemInfo(String id,String menssagem){
        FacesContext.getCurrentInstance().addMessage(id, new FacesMessage(FacesMessage.SEVERITY_INFO,menssagem,""));        
    }        
    
    public static void setAtributoSessao(String nome, Object atributo){
        FacesContext fc = FacesContext.getCurrentInstance();  
        HttpSession session = (HttpSession) fc.getExternalContext().getSession(true);  
        session.setAttribute(nome, atributo );  
    }

    public static Object getAtributoSessao(String nome){
        FacesContext fc = FacesContext.getCurrentInstance();  
        HttpSession session = (HttpSession) fc.getExternalContext().getSession(true);
        Object retorno = session.getAttribute(nome);
        return retorno;
    }
    
    public static void logoff(){
        ExternalContext ectx = FacesContext.getCurrentInstance().getExternalContext();
        HttpServletResponse response = (HttpServletResponse)ectx.getResponse();
        HttpSession session = (HttpSession)ectx.getSession(false);
        session.invalidate();
    }
    
    public static void redirecionar(String endereco) throws IOException{
        FacesContext.getCurrentInstance().getExternalContext().redirect(FacesContext.getCurrentInstance().getExternalContext().getRequestContextPath()+"/"+endereco); 
    }
}
